<template>
  <div class='container'>
    <el-dialog :title="title" :visible="showDialog" width="30%" :before-close="handleClose" @open="getSubjectList">
      <el-form :rules="ruleInline" :model="subject" ref="addRef">
        <el-form-item label="学科名称" prop="subjectName" label-position="left" label-width="80px">
          <el-input placeholder="请输入学科名称" v-model="subject.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" style="margin-left:12px">
          <el-switch v-model="subject.isFrontDisplay" inactive-color="#ff4949" active-color="#13ce66" active-value="1" inactive-value="0">
          </el-switch>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { add, update, detail } from '../../api/hmmm/subjects'
export default {
  // name: 'SubjectsAdd',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    subjectsId: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      // 表单内容
      subject: {
        subjectName: '',
        isFrontDisplay: '1'
      },

      // 验证规则
      ruleInline: {
        subjectName: [{
          required: true, message: '请输入学科名称', trigger: ['blur', 'change']
        }]

      }

    }
  },
  computed: {
    title () {
      if (this.subjectsId !== 0) {
        return '修改学科'
      } else {
        return '添加学科'
      }
    }
  },
  methods: {
    // 关闭弹窗
    handleClose () {
      this.$emit('update:showDialog', false)
      this.subject = this.$options.data().subject
      this.$refs.addRef.resetFields()
    },
    // 提交
    handleSubmit () {
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        // 验证成功，调用接口
        if (this.subjectsId !== 0) {
          // 修改学科
          try {
            const ret = await update(this.subject)
            if (ret.status === 200) {
              // 添加成功，刷新列表，关闭弹窗，清空表单
              this.$message.success('修改学科成功')
              this.$parent.loadDataList()
              this.$emit('update:showDialog', false)
              this.$refs.addRef.resetFields()
              this.subject = this.$options.data().subject
            } else {
              this.$message.error('修改学科失败')
            }
          } catch {
            this.$message.error('修改学科失败')
          }
        } else {
          // 添加学科
          try {
            const ret = await add(this.subject)
            if (ret.status === 200) {
              // 添加成功，刷新列表，关闭弹窗，清空表单
              this.$message.success('添加学科成功')
              this.$parent.loadDataList()
              this.$emit('update:showDialog', false)
              this.$refs.addRef.resetFields()
            } else {
              this.$message.error('添加学科失败')
            }
          } catch {
            this.$message.error('添加学科失败')
          }
        }
      })
    },
    async getSubjectList () {
      if (this.subjectsId !== 0) {
        const ret = await detail({
          id: this.subjectsId
        })
        if (ret.status === 200) {
          this.subject = ret.data
        }
        console.log(ret)
      }
    }
  }

}

</script>

<style scoped lang='less'></style>
